import Food from './Food';
import ScorePanel from './ScorePanel';
import Snake from './Snake';

class GameControl {
  snake: Snake;
  food: Food;
  scorePanel: ScorePanel;

  //键盘按下的方向，即蛇运动的方向
  // direction: string = '';
  // 创建一个属性用来记录游戏是否结束
  isLive: boolean = false;
  // 是否允许修改方向(解决在定时器时间间隔之内修改方法时，产生蛇掉头bug)
  isAllowModification: boolean = true;

  constructor() {
    this.snake = new Snake();
    this.food = new Food();
    this.scorePanel = new ScorePanel();

    this.handleInit();
  }

  // 初始化游戏
  handleInit() {
    // 需要还变this的指向，不然this就指向document
    document.addEventListener('keydown', this.handleKeydown.bind(this));
    this.snake.direction = 'ArrowRight';
    console.log(this.snake.direction);
    this.handleRun();
  }

  // 开始游戏
  handleStartGame() {
    if (!this.isLive) {
      this.isLive = true;
      this.snake.direction = 'ArrowRight';
      console.log(this.snake.direction);
      this.handleRun();
    }
  }

  // 暂停游戏
  handleStopGame() {
    this.isLive = false;
  }

  // 继续游戏
  handleContinueGame() {
    if (!this.isLive) {
      this.isLive = true;
      this.handleRun();
    }
  }

  // 键盘按下的事件
  handleKeydown(event: KeyboardEvent): void {
    if (event.key === 's') {
      this.handleStartGame();
    } else if (event.key === 'p') {
      this.handleStopGame();
    } else if (event.key === 'c') {
      this.handleContinueGame();
    } else {
      // TODO 检查按下的按钮是否有效
      const key = event.key;
      if (!this.isAllowModification) return;
      this.isAllowModification = false;
      if (['ArrowUp', 'ArrowRight', 'ArrowDown', 'ArrowLeft'].includes(key)) {
        if (!this.snake.handleCheckDirection(key)) return;
        //如果按的太快direction的值被修改了
        this.snake.direction = key;
      }
    }
  }

  // 控制蛇移动的方法
  handleRun() {
    if (!this.isLive) return;
    this.isAllowModification = true;
    /**
     * 向上top 减少
     * 向下top增加
     * 向左left减少
     * 向右增加
     */
    // let X = this.snake.X;
    // let Y = this.snake.Y;
    console.log(this.snake.direction);

    // 当前蛇的位置
    const currentLocation = this.snake.handleMoveLocationSize(
      this.snake.X,
      this.snake.Y,
      10
    );
    this.handleEatFood(currentLocation.X, currentLocation.Y);

    try {
      // 修改蛇的XY值
      if (this.snake.X !== currentLocation.X) this.snake.X = currentLocation.X;
      if (this.snake.Y !== currentLocation.Y) this.snake.Y = currentLocation.Y;
    } catch (e) {
      this.isLive = false;
      alert(e.message + ',游戏结束');
      return;
    }

    this.isLive &&
      setTimeout(
        this.handleRun.bind(this),
        500 - (this.scorePanel.level - 1) * 30
      );
  }

  // 吃食物
  handleEatFood(X: number, Y: number): boolean {
    if (X === this.food.X && this.food.Y == Y) {
      console.log('吃到食物了');
      // 重置食物的位置
      this.food.handleChangePosition();
      // 增加分数
      this.scorePanel.handleAddScore();
      // 蛇要增加一节
      this.snake.handleAddBody();
      return true;
    }
    return false;
  }
}

export default GameControl;
